﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class SetUserMenuQControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        MPRepeater MPRepeaterList;
        string userid;
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            MPRepeaterList = (MPRepeater)FindControl("MPRepeaterList");
            userid = base.Page.Request["UserID"];
            if (!base.Page.IsPostBack) {
                Bind();
            }
            Button saveset = (Button)FindControl("saveset");
            saveset.Click += new EventHandler(saveset_Click);
        }
        protected void Bind()
        {
            MPRepeaterList.ItemDataBound += new RepeaterItemEventHandler(MPRepeaterList_ItemDataBound);
            DataTable dt = null;
            if (_EluZSDataProvider.IsNetZSAdmin(userid) || _EluZSDataProvider.IsZSAdmin(userid))
            {
                dt = _EluZSDataProvider.SetUserMenuQ("0", userid,"","");
            }
            else
            {
                dt = _EluZSDataProvider.SetUserMenuQ("1", userid,"","");
            }
            MPRepeaterList.DataSource = dt;
            MPRepeaterList.DataBind();
        }

        protected void MPRepeaterList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label showtr = (Label)e.Item.FindControl("showtr");
            Label idtib = (Label)e.Item.FindControl("idtib");
            if ((e.Item.ItemIndex + 1) % 2 == 0)
            {
                showtr.Text = "</tr><tr>";
            }
            MPRepeater sMPRepeaterList = (MPRepeater)e.Item.FindControl("sMPRepeaterList");
            DataTable dt = _EluZSDataProvider.SetUserMenuQ("2", userid, idtib.Text.Trim(), GetSqlWhere());
            sMPRepeaterList.DataSource = dt;
            sMPRepeaterList.DataBind();
        }

        protected void saveset_Click(object sender, EventArgs e)
        {
            string strid = base.Page.Request["ID"];
            for (int temp = 0; temp < MPRepeaterList.Items.Count; temp++)
            {
                MPRepeater sMPRepeaterList = (MPRepeater)MPRepeaterList.Items[temp].FindControl("sMPRepeaterList");
                for (int index = 0; index < sMPRepeaterList.Items.Count; index++)
                {
                    Label sidtib = (Label)sMPRepeaterList.Items[index].FindControl("sidtib");
                    CheckBox checkselect = (CheckBox)sMPRepeaterList.Items[index].FindControl("checkselect");
                    int usercount = Convert.ToInt32(_EluZSDataProvider.GetUserMenuQ(userid, sidtib.Text.Trim()));
                    if (checkselect.Checked)
                    {
                        if (usercount == 0)
                        {
                            _EluZSDataProvider.UpdateUserMenuQ("0", userid, sidtib.Text.Trim());
                        }
                        else if (usercount > 0) { 
                            
                        }
                    }
                    else
                    {
                        if (usercount > 0)
                        {
                            _EluZSDataProvider.UpdateUserMenuQ("1", userid, sidtib.Text.Trim());
                        }
                        else if (usercount == 0) { 
                            
                        }
                    }
                }
            }
            base.Page.Response.Redirect("UserAuthorization.aspx");
        }

        protected string GetSqlWhere() {
            string sqlstr = "";
            if (!_EluZSDataProvider.IsNetZSAdmin(userid) && !_EluZSDataProvider.IsZSAdmin(userid))
            {
                sqlstr = @"  And Id In(
                      Select Distinct menuid From (
                Select  Menuid
                From zs_Menu_In_User
                Where Userid = '" + userid + @"'
                Union
                Select distinct menuid From zs_Menu_In_Role Where roleid In(
                Select roleid From zs_User_In_Role Where userid='" + userid + @"'))     
                            )  ";
//                sqlstr = @" And Id In( Select Distinct menuid From (
//Select  Menuid From zs_Menu_In_User Where Userid = '" + userid + @"')) ";
            }
            return sqlstr;
        }
    }
}


